import * as  cc from 'cc'
import { PokerIntensify } from "./PokerIntensify";
import { PokerConfig } from '../config/PokerConfig';


export default class PokerItem {

    readonly pokerIntensify: PokerIntensify;
    readonly pokerNode: cc.Node;

    readonly pokerImage: cc.Node;
    readonly pokerActive: cc.Node;

    constructor(pokerIntensify: PokerIntensify, pokerNode: cc.Node, isBack = true) {
        this.pokerIntensify = pokerIntensify;
        this.pokerNode = pokerNode;

        // 添加扑克的背景
        const pokerImage = pokerNode.getChildByName('PokerImage');
        if (!pokerImage) {
            throw ' Poker Node need has PokerInage Node please check'
        }
        this.pokerImage = pokerImage;

        // 添加poker active时,也就是算分的动画
        const pokerActive = pokerNode.getChildByName('PokerActive');
        if (!pokerActive) {
            throw ' Poker Node need has pokerActive Node please check'
        }
        this.pokerActive = pokerActive;

        cc.resources.load(pokerIntensify.path, cc.ImageAsset, (err, result) => {
            if (!pokerNode.isValid) return;
            // 添加PokerTexture
            const spriteComp = pokerImage.getComponent(cc.Sprite);
            if (!spriteComp) return;
            const frame = spriteComp.spriteFrame
            if (!frame) return;
            const texture = cc.SpriteFrame.createWithImage(result);
            // 设置大小
            texture.rect = cc.math.rect((result.width - frame.rect.width) / 2,
                (result.height - frame.rect.height) / 2,
                frame.rect.width,
                frame.rect.height);
            spriteComp.spriteFrame = texture;
            // 添加加强
            const widgetComp = pokerNode.getComponentInChildren(cc.Widget);
            if (!widgetComp) return;
            pokerIntensify.draw(pokerNode, widgetComp);
        })
    }

    changeActive(activePath: string) {
        //添加一个动画 poker 需要挑一下, 然后顶起一个 当前的效果
        // 如果是同花 , 则显示顶起当前的花色
        // 如果是同色, 则显示当前的颜色
        // 如果是链子,则显示当前的链子图标
        // 如果是对子,则显示当前的对子图标

        // poker 调一下,达到最高(call),显示一个 色块,并且当回落后(call),色块变成能量 输送到上方的数值条中,当进入数值条后(call),数字进行变化
        const anima = this.pokerNode.getComponent(cc.Animation);
        if (!anima) {
            return;
        }

        const spriteComp = this.pokerActive.getComponent(cc.Sprite);
        if (!spriteComp) {
            return;
        }

        // 加载指定的图
        cc.resources.load(activePath, cc.ImageAsset, (err, result) => {
            const frame = spriteComp.spriteFrame
            if (!frame) return;

            console.log(`result ${result}`)
            const texture = cc.SpriteFrame.createWithImage(result);
            // 设置大小
            // texture.rect = cc.math.rect((result.width - frame.rect.width) / 2,
            //     (result.height - frame.rect.height) / 2,
            //     frame.rect.width,
            //     frame.rect.height);
            spriteComp.spriteFrame = texture;


            // 监听算分的动画显示 , 更新其算分image的图案
            this.pokerNode.once('run_active', () => {
                // 显示同花
                // 显示同色
                // 显示链子
                // 显示对子
                console.log(' animal run_active ', anima.hasEventListener('run_top'))

            })

            // 监听动画结束
            anima.once(cc.Animation.EventType.FINISHED, () => {
                console.log(' animal finish ', anima.hasEventListener('run_top'))
            })

            // 显示动画
            anima.play('PokerActive');
        })


    }

    changeSelect(isSelect: boolean) {
        if (!this.pokerNode.parent) return;
        this.pokerNode.y = isSelect ? 20 : 0;
    }

    getPoker() {
        return this.pokerIntensify.poker;
    }

    getIntensify() {
        return this.pokerIntensify.intensify;
    }

    getNumber() {
        return PokerConfig.pokerNumber(this.getPoker())
    }
}